package com.xjrsoft.module.version.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.version.dto.AddXjrPublishDto;
import com.xjrsoft.module.version.dto.UpdateXjrPublishDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.version.dto.XjrPublishPageDto;
import com.xjrsoft.module.version.entity.XjrPublish;
import com.xjrsoft.module.version.service.IPublishService;
import com.xjrsoft.module.version.vo.XjrPublishPageVo;

import com.xjrsoft.module.version.vo.XjrPublishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @title: 发版管理
* @Author 管理员
* @Date: 2024-07-11
* @Version 1.0
*/
@RestController
@RequestMapping("/version" + "/publish")
@Api(value = "/version"  + "/publish",tags = "发版管理代码")
@AllArgsConstructor
public class PublishController {


    private final IPublishService publishService;

    @GetMapping(value = "/page")
    @ApiOperation(value="XjrPublish列表(分页)")
    @SaCheckPermission("publish:detail")
    public R page(@Valid XjrPublishPageDto dto){

        LambdaQueryWrapper<XjrPublish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getVersionNo()),XjrPublish::getVersionNo,dto.getVersionNo())
                .between(ObjectUtil.isNotNull(dto.getPublishDateStart()) && ObjectUtil.isNotNull(dto.getPublishDateEnd()),XjrPublish::getPublishDate,dto.getPublishDateStart(),dto.getPublishDateEnd())
                .like(StrUtil.isNotBlank(dto.getPublisher()),XjrPublish::getPublisher,dto.getPublisher())
                .eq(ObjectUtil.isNotNull(dto.getPublishStatus()),XjrPublish::getPublishStatus,dto.getPublishStatus())
                .like(StrUtil.isNotBlank(dto.getUserArea()),XjrPublish::getUserArea,dto.getUserArea())
                .like(StrUtil.isNotBlank(dto.getPublishRemark()),XjrPublish::getPublishRemark,dto.getPublishRemark())
                    .orderByDesc(XjrPublish::getId)
                .select(XjrPublish.class,x -> VoToColumnUtil.fieldsToColumns(XjrPublishPageVo.class).contains(x.getProperty()));
        IPage<XjrPublish> page = publishService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<XjrPublishPageVo> pageOutput = ConventPage.getPageOutput(page, XjrPublishPageVo.class);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询XjrPublish信息")
    @SaCheckPermission("publish:detail")
    public R info(@RequestParam Long id){
        XjrPublish xjrPublish = publishService.getById(id);
        if (xjrPublish == null) {
           return R.error("找不到此数据！");
        }
        return R.ok(BeanUtil.toBean(xjrPublish, XjrPublishVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增XjrPublish")
    @SaCheckPermission("publish:add")
    public R add(@Valid @RequestBody AddXjrPublishDto dto){
        XjrPublish xjrPublish = BeanUtil.toBean(dto, XjrPublish.class);
        boolean isSuccess = publishService.save(xjrPublish);
        return R.ok(xjrPublish.getId());
    }

    @PutMapping
    @ApiOperation(value = "修改XjrPublish")
    @SaCheckPermission("publish:edit")
    public R update(@Valid @RequestBody UpdateXjrPublishDto dto){

        XjrPublish xjrPublish = BeanUtil.toBean(dto, XjrPublish.class);
        return R.ok(publishService.updateById(xjrPublish));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission("publish:delete")
    public R delete(@Valid @RequestBody List<Long> ids){
        return R.ok(publishService.removeBatchByIds(ids));

    }

}